package com.liangxy.zhyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liangxy.zhyl.base.PageResponse;
import com.liangxy.zhyl.dto.NursingLevelDto;
import com.liangxy.zhyl.entity.NursingLevel;
import com.liangxy.zhyl.mapper.NursingLevelMapper;
import com.liangxy.zhyl.service.NursingLevelService;
import com.liangxy.zhyl.utils.UserThreadLocal;
import com.liangxy.zhyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public List<NursingLevelVo> listAll() {
        List<NursingLevelVo> nursingLevelVos = nursingLevelMapper.listAll();
        return nursingLevelVos;
    }
    /**
     * 添加护理等级
     * @param nursingLevelDto
     */
    @Override
    public void insert(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
        nursingLevel.setLplanId(nursingLevelDto.getPlanId());
        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 条件分页查询护理等级
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> page(Integer pageNum, Integer pageSize, String name, Integer status) {
        // 初始化
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.nursingLevelPage(name, status);
        return PageResponse.of(page,NursingLevelVo.class);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo selectById(Long id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        return nursingLevelVo;
    }

    /**
     * 更新护理等级
     * @param nursingLevelDto
     */
    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
        nursingLevel.setLplanId(nursingLevelDto.getPlanId());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevel.setUpdateBy(UserThreadLocal.getUserId());
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 删除护理等级
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingLevelMapper.deleteById(id);
    }
    /**
     * 启用禁用护理等级
     * @param id
     * @param status
     */
    @Override
    public void stopAndStart(Long id, Integer status) {
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setId(id);
        nursingLevel.setStatus(status);
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevel.setUpdateBy(UserThreadLocal.getMgtUserId());
        nursingLevelMapper.update(nursingLevel);
    }

}
